//
//  XinagqingCollectionViewCell.m
//  B段项目
//
//  Created by 徐茂怀 on 15/11/4.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "XinagqingCollectionViewCell.h"
#import "XMHNetWorkongMethods.h"
#import "FitHeader.h"
#import "SingleTon.h"
#import "XiangqingHeaderView.h"
#import <UIImageView+WebCache.h>
#import "XiangqingTableViewCell.h"
#import "XiangqingFooterView.h"
#import "ShiwuViewController.h"
@implementation XinagqingCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self getData];
        [self createView];
}
    return self;
}

-(void)createView
{
    _tableView = [[UITableView alloc]init];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    UIView *v = [[UIView alloc]initWithFrame:CGRectZero];
//    _tableView.tableFooterView = v;

    [self.contentView addSubview:_tableView];
}

-(void)getData
{
    [XMHNetWorkongMethods getDataVyString:@"http://api.haodou.com/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1446620759847&vc=77&vn=5.4.0&loguid=0&deviceid=haodou99000505722929&uuid=a6afb132369a979ba3427eec69189063&channel=xiaomi_v540&method=Info.getInfo&virtual=&signmethod=md5&v=2&timestamp=1446622896&nonce=0.2484944032255545&appsign=3e42c1c2bff643b1e91b1541bb1a2e37" BodyString:[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"uid",[SingleTon shareSingleTon].rid,@"rid",@"a6afb132369a979ba3427eec69189063",@"uuid", nil] WithDataBlock:^(id data) {
        NSDictionary *dic = [data objectForKey:@"result"];
        self.dataDic = [dic objectForKey:@"info"];
        XiangqingHeaderView *headerView = [[XiangqingHeaderView alloc]initWithFrame:CGRectMake(0, 0, 375 * FitWidth, 220 * FitHeight)];
        headerView.titleLabel.text = [_dataDic objectForKey:@"Title"];
        headerView.userNameLabel.text = [_dataDic objectForKey:@"UserName"];
        headerView.timeLabel.text = [NSString stringWithFormat:@"发布于%@",[_dataDic objectForKey:@"ReviewTime"]];
        headerView.contentLabel.text = [_dataDic objectForKey:@"Intro"];
        [headerView.MyImageView sd_setImageWithURL:[NSURL URLWithString:[_dataDic objectForKey:@"Avatar"]]];
        _tableView.tableHeaderView = headerView;
        
        XiangqingFooterView *footerView = [[XiangqingFooterView alloc]initWithFrame:CGRectMake(0, 0, 375 * FitWidth, (240 + [self measureHeightWithString:[_dataDic objectForKey:@"Tips"]]) * FitHeight)];
        footerView.timeLabel.text = [NSString stringWithFormat:@"制作时间:%@",[_dataDic objectForKey:@"CookTime"]];
        footerView.renshuLabel.text = [NSString stringWithFormat:@"用餐人数:%@",[_dataDic objectForKey:@"UserCount"]];
        footerView.contentLabel.text = [_dataDic objectForKey:@"Tips"];
        _tableView.tableFooterView = footerView;
        [_tableView reloadData];
    }];
}

-(CGFloat)measureHeightWithString:(NSString *)string
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17 * FitWidth], NSFontAttributeName, nil];
    CGRect rect = [string boundingRectWithSize:CGSizeMake(self.frame.size.width - 20 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _tableView.frame = CGRectMake(0, 0, 375 * FitWidth, self.contentView.frame.size.height - 64);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_dataDic objectForKey:@"Stuff"]count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45 * FitHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    XiangqingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[XiangqingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.nameLabel.text = [[[_dataDic objectForKey:@"Stuff"]objectAtIndex:indexPath.row]objectForKey:@"name"];
    cell.weightLabel.text = [[[_dataDic objectForKey:@"Stuff"]objectAtIndex:indexPath.row]objectForKey:@"weight"];
    if ([[[[[_dataDic objectForKey:@"Stuff"]objectAtIndex:indexPath.row]objectForKey:@"food_flag"]stringValue]isEqualToString:@"1"]) {
        [cell.haveButton setImage:[UIImage imageNamed:@"iconfont-gantanhao.png"] forState:0];
    }
    else
    {
        [cell.haveButton setImage:nil forState:0];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[[[[_dataDic objectForKey:@"Stuff"]objectAtIndex:indexPath.row]objectForKey:@"food_flag"]stringValue]isEqualToString:@"1"]) {
        [self.delegate pushToFood:[[[[_dataDic objectForKey:@"Stuff"]objectAtIndex:indexPath.row]objectForKey:@"id"] stringValue] Name:[[[_dataDic objectForKey:@"Stuff"]objectAtIndex:indexPath.row]objectForKey:@"name"]];
    }
}


@end
